package io.xxx.xbutler.web.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.domain.task.TaskTemplate;
import io.xxx.xbutler.domain.task.TaskTemplateItem;
import io.xxx.xbutler.domain.task.TaskTemplatePeriod;
import io.xxx.xbutler.service.TaskTemplateService;
import io.xxx.xbutler.web.controller.form.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/task/template")
public class TaskTemplateController {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private TaskTemplateService templateService;

    @GetMapping("/list")
    public Page<TaskTemplate> list(TaskTemplate template,
                                   @RequestParam(defaultValue = "1") Integer pageNumber,
                                   @RequestParam(defaultValue = "20") Integer pageSize) {
        log.info("查询任务模板列表[{}]", JSON.toJSONString(template));
        return templateService.list(template, new Page<>(pageNumber, pageSize));
    }

    @GetMapping("/get")
    public TaskTemplate get(@RequestParam Long id) {
        log.info("查询任务模板详情[id:{}]", id);
        return templateService.get(id);
    }

    @PostMapping("/create")
    public Long create(@RequestBody @Valid CreateTaskTemplateForm form) {
        log.info("创建任务模板[{}]", JSON.toJSONString(form));
        Long templateId = idGenerator.next();
        LocalDateTime now = LocalDateTime.now();
        Triple<TaskTemplate, List<TaskTemplatePeriod>, List<TaskTemplateItem>> triple = build(templateId, form, now);
        TaskTemplate template = triple.getLeft();
        template.setCreatedTime(now);
        templateService.create(template, triple.getMiddle(), triple.getRight());
        return templateId;
    }

    @PutMapping("/update")
    public void update(@RequestBody @Valid UpdateTaskTemplateForm form) {
        log.info("更新任务模板[{}]", JSON.toJSONString(form));
        Long templateId = form.getId();
        LocalDateTime now = LocalDateTime.now();
        Triple<TaskTemplate, List<TaskTemplatePeriod>, List<TaskTemplateItem>> triple = build(templateId, form, now);
        TaskTemplate template = triple.getLeft();
        template.setUpdatedTime(now);
        templateService.update(template, triple.getMiddle(), triple.getRight());
    }

    private Triple<TaskTemplate, List<TaskTemplatePeriod>, List<TaskTemplateItem>> build(Long templateId,
                                                                                         TaskTemplateForm form,
                                                                                         LocalDateTime now) {
        List<TaskTemplatePeriodForm> periodForms = form.getPeriods();
        List<TaskTemplateItemForm> itemForms = form.getItems();

        TaskTemplate template = new TaskTemplate();
        template.setId(templateId);
        BeanUtils.copyProperties(form, template);

        List<TaskTemplatePeriod> periods = new ArrayList<>(periodForms.size());
        for (int i = 0; i < periodForms.size(); i++) {
            TaskTemplatePeriodForm periodForm = periodForms.get(i);
            TaskTemplatePeriod period = new TaskTemplatePeriod();
            BeanUtils.copyProperties(periodForm, period);
            period.setId(idGenerator.next());
            period.setTemplateId(templateId);
            period.setSn(i);
            period.setCreatedTime(now);
            periods.add(period);
        }

        List<TaskTemplateItem> items = new ArrayList<>(itemForms.size());
        for (int i = 0; i < itemForms.size(); i++) {
            TaskTemplateItemForm itemForm = itemForms.get(i);
            TaskTemplateItem item = new TaskTemplateItem();
            BeanUtils.copyProperties(itemForm, item);
            item.setId(idGenerator.next());
            item.setTemplateId(templateId);
            item.setSn(i);
            item.setCreatedTime(now);
            items.add(item);
        }
        return Triple.of(template, periods, items);
    }

    @DeleteMapping("/delete")
    public void delete(@RequestParam Long id) {
        log.info("删除任务模板[{}]", id);
        templateService.delete(id);
    }
}
